import React from 'react';
import {connect} from 'dva/index';
import {Button, Col, Form, Input, Popconfirm, Row, Switch, Table} from 'antd';
import Constants from '../../utils/Constants';
import RightConstants from '../../utils/RightConstants';
import RightBox from '../../components/common/rightBox';
import DeclareFeeDetail from '../../components/fee/declareFee/declareFeeDetail';
import ImportFile from '../../components/common/importFile';
import PortSelect from '../../components/common/portSelect';

const FormItem = Form.Item;

const declareFee = Form.create()(class DeclareFee extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.props.dispatch({ type: 'declareFee/getListForPage', payload: { page: Constants.DEFAULT_PAGE } });
    }

    handleOpen = (id, disabled) => {
        this.props.dispatch({
            type: 'declareFee/getById',
            payload: { id }
        });
        this.props.dispatch({ type: 'supplier/getSupplierList', payload: { supplierType: Constants.SUPPLIER_TYPE.DECLARE } });  //查询报关供应商
        this.declareFeeDetail.handleOpen(disabled);
    }

    handleImport = (module) =>{
        this.importFile.handleOpen(module);
    }

    onSwitch = (id, checked) => {
        this.props.dispatch({
            type: 'declareFee/updateDeclareFee',
            payload: {
                id,
                status: checked ? "1" : "0",
                onSuccess: this.handleSearch
            }
        })
    }

    handleDelete = id => {
        this.props.dispatch({
            type: 'declareFee/deleteById',
            payload: {
                id,
                onSuccess: this.handleSearch
            }
        })
    }

    handleSearch = flag => {
        let newPage = this.props.declareFee.page;
        if (flag) newPage.page = 1;
        this.props.dispatch({
            type: 'declareFee/getListForPage',
            payload: {
                ...this.props.form.getFieldsValue(),
                page: newPage
            }
        });
    };


    columns = [
        { title: '供应商名称/代码', key: 'companyname',
            render: record =>{
                return <span>{record.companyname}({record.supplierCode})</span>
            }    
        },
        { title: '出货港口', 
            render: record =>{
                return <span>{record.portEnName}({record.portName})</span>
            }
        },
        { title: '报关费', dataIndex: 'declareFee', key: 'declareFee' },
        { title: '港建费', dataIndex: 'portConstructionFee', key: 'portConstructionFee' },
        // { title: '港杂费', dataIndex: 'portMiscellaneousFee', key: 'portMiscellaneousFee' },
        { title: '有效开始时间', dataIndex: 'begindate', key: 'begindate' },
        { title: '有效截止时间', dataIndex: 'enddate', key: 'enddate' },
        {
            title: '生效中',
            render: (record) => {
                return (
                    <Switch checkedChildren={'是'} unCheckedChildren={'否'} checked={record.status === "1"} onChange={this.onSwitch.bind(this, record.id)} />
                )
            }
        },
        {
            title: '操作',
            render: (record) => {
                let editLink = <RightBox resource={RightConstants.COSTMAINTENANCE.COSTMAINTENANCE_DECLAREFEE_EDIT} split={true}><a href="#" onClick={this.handleOpen.bind(this, record.id, false)}>编辑</a></RightBox>
                let deleteLink = <Popconfirm onConfirm={this.handleDelete.bind(this, record.id)} okText="是" cancelText="否"
                    placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除?'}>
                    <a href="#">删除</a>
                </Popconfirm>
                deleteLink = <RightBox resource={RightConstants.COSTMAINTENANCE.COSTMAINTENANCE_DECLAREFEE_DELETE} split={true}>{deleteLink}</RightBox>
                let viewLink = <a href="#" onClick={this.handleOpen.bind(this, record.id, true)}>查看</a>
                return <span>{editLink}{deleteLink}{viewLink}</span>
            }
        }
    ];

    render() {
        let formItemLayout = { labelCol: { span: 8 }, wrapperCol: { span: 16 } };
        const { getFieldDecorator } = this.props.form;
        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            pageSize: this.props.declareFee.page.size,
            current: this.props.declareFee.page.page,
            total: this.props.declareFee.page.totalRow,
            showSizeChanger: true,
            onShowSizeChange: (current, size) => {
                let newPage = this.props.declareFee.page;
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'declareFee/getListForPage',
                    payload: {
                        ...this.props.form.getFieldsValue(),
                        page: newPage
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = this.props.declareFee.page;
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'declareFee/getListForPage',
                    payload: {
                        ...this.props.form.getFieldsValue(),
                        page: newPage
                    }
                });
            }
        }

        return (
            <div>
                <Row>
                    <Col span={7}>
                        <FormItem {...formItemLayout} label={'供应商'}>
                            {getFieldDecorator('supplierCode')
                            (
                                <Input maxLength="20" placeholder="可根据供应商代码、中英文名称搜索" />
                            )}
                        </FormItem>
                    </Col>
                    <Col span={7}>
                        <FormItem {...formItemLayout} label={'出货港口'}>
                            {getFieldDecorator('port')
                                (<PortSelect style={{width: '100%'}} portType={"1"} allowClear/>)}
                        </FormItem>
                    </Col>
                    <Col span={9} style={{ marginLeft: 24}}>
                        <Button type="primary" icon="search" style={{ marginRight: 5 }} onClick={this.handleSearch.bind(this, true)} size={'default'}>查询</Button>
                        <RightBox resource={RightConstants.COSTMAINTENANCE.COSTMAINTENANCE_DECLAREFEE_ADD}>
                            <Button type="primary" icon="plus" style={{ marginRight: 5 }} onClick={this.handleOpen.bind(this, null, false)} size={'default'}>   新增报关费</Button>
                        </RightBox>
                        <RightBox resource={RightConstants.COSTMAINTENANCE.COSTMAINTENANCE_DECLAREFEE_IMPORT}>
                            <Button type="primary" icon="plus" style={{ marginRight: 5 }} onClick={this.handleImport.bind(this, Constants.IMPORT_MODULE[5])} size={'default'}>导入报关费</Button>
                        </RightBox>
                    </Col>
                </Row>
                <Table
                    rowKey='id'
                    dataSource={this.props.declareFee.list}
                    loading={this.props.loading}
                    columns={this.columns}
                    bordered={true}
                    size={'middle'}
                    title={() => <span style={{ color: 'red' }}>*注：报关费目前同时只允许一条生效, 币种默认为CNY</span>}
                    pagination={pagination} />
                <DeclareFeeDetail wrappedComponentRef={(inst) => this.declareFeeDetail = inst} refreshList={this.handleSearch} />
                <ImportFile wrappedComponentRef={(inst) => this.importFile = inst} refreshList={this.handleSearch} />
            </div>
        );
    }
})

function mapStateToProps(state) {
    return {
        loading: state.loading.models.declareFee, declareFee: state.declareFee
    };
}

export default connect(mapStateToProps)(declareFee);
